<?php 

class Enrollment_Model extends grocery_CRUD_Model 
{
    
    /*
     * Realiza la consulta para obtener datos de la tabla seleccioanda
     */
    function updateCounter($data, $id){           
        $this->db->where('CON_SERIAL', $id);
        $this->db->update('CONTADOR', $data);
    }
    
    //obtener los contadores que estan disponibles
    function getListCounterEnables()
    {        
        $query ="SELECT * FROM CONTADOR WHERE CON_ESTADO='D'";
        $results = $this->db->query($query)->result();
        $counters=array();
        foreach ($results as $counter)
        {
            $counters[$counter->CON_SERIAL] = $counter->CON_SERIAL;
        }
	return $counters;        
    }
    
    function getListEnrollmentUserById($id)
    {        
        $query ="SELECT M.MAT_ID, M.MAT_DIRECCIONINSTALACION, M.CON_SERIAL
        FROM  MATRICULA AS M, USUARIOMATRICULACONEXION UM 
        WHERE M.MAT_ID= UM.MAT_ID AND UM.UMC_ESTADO='A' AND UM.USUA_ID=".$id;
        $results = $this->db->query($query)->result();
	return $results;
        
    }
    
//    function get_list()
//    {
//    	if($this->table_name === null)
//    		return false;
//               
//    	$select = "{$this->table_name}.*";
//        $select .= ",USUARIOMATRICULACONEXION.MAT_ID,USUARIOMATRICULACONEXION.USUA_ID,USUARIOMATRICULACONEXION.UMC_FECHAMATRICULA,USUARIOMATRICULACONEXION.UMC_FECHADESACTIVACION,USUARIOMATRICULACONEXION.UMC_ESTADO,USUARIOMATRICULACONEXION.UMC_MOTIVO";
//        $this->db->join('USUARIOMATRICULACONEXION','USUARIOMATRICULACONEXION.MAT_ID = MATRICULA.MAT_ID');
//    	$this->db->select($select, false);
//    	$results = $this->db->get($this->table_name)->result();	
//    	return $results;
//    } 
    
    function get_edit_values($primary_key_value)
    {
        $select = "{$this->table_name}.*";
        $select .= ",USUARIOMATRICULACONEXION.USUA_ID,USUARIOMATRICULACONEXION.UMC_FECHAMATRICULA,USUARIOMATRICULACONEXION.UMC_FECHADESACTIVACION,USUARIOMATRICULACONEXION.UMC_ESTADO,USUARIOMATRICULACONEXION.UMC_MOTIVO";
        $this->db->where('MATRICULA.MAT_ID',$primary_key_value);
        $this->db->join('USUARIOMATRICULACONEXION','USUARIOMATRICULACONEXION.MAT_ID = MATRICULA.MAT_ID');
    	$this->db->select($select, false);	
        $result = $this->db->get($this->table_name)->row();
    	return $result;
    }
    
    function get_field_types_basic_table()
    {
    	$db_field_types = array();
        $db_query_result_enrollment=$this->db->query("SHOW COLUMNS FROM `{$this->table_name}`")->result();
        $db_query_result_connect=$this->db->query("SHOW COLUMNS FROM USUARIOMATRICULACONEXION")->result();
        $db_merge_result = array_merge((array)$db_query_result_enrollment,(array)$db_query_result_connect);
    	foreach($db_merge_result as $db_field_type)
    	{
    		$type = explode("(",$db_field_type->Type);
    		$db_type = $type[0];
    		
    		if(isset($type[1]))
    		{
    			if(substr($type[1],-1) == ')')
    			{
    				$length = substr($type[1],0,-1);
    			}
    			else
    			{
    				list($length) = explode(" ",$type[1]);
    				$length = substr($length,0,-1);
    			}
    		}
    		else 
    		{
    			$length = '';
    		}
    		$db_field_types[$db_field_type->Field]['db_max_length'] = $length;
    		$db_field_types[$db_field_type->Field]['db_type'] = $db_type;
    		$db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false;
    		$db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra;
    	}

    	$results_enrollment = $this->db->field_data($this->table_name);
        $results_connect = $this->db->field_data("USUARIOMATRICULACONEXION");        
        $results=array_merge( (array)$results_enrollment,(array)$results_connect);
    	foreach($results as $num => $row)
    	{
            
    		$row = (array)$row;
    		$results[$num] = (object)( array_merge($row, $db_field_types[$row['name']])  );
    	}
    	
    	return $results;
    }

    function db_update($post_array, $primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
        $enrollment_data=array();
        $connect_data=array();        
        
        $enrollment_data['EST_ID'] = $post_array['EST_ID'];        
        $enrollment_data['MAT_DIRECCIONINSTALACION'] = $post_array['MAT_DIRECCIONINSTALACION'];
        $enrollment_data['MAT_ESTADOCONEXION'] = $post_array['MAT_ESTADOCONEXION'];
        $enrollment_data['MAT_ESTADOMATRICULA'] = $post_array['MAT_ESTADOMATRICULA'];
        $enrollment_data['TM_ID'] = $post_array['TM_ID'];
        
        $connect_data['UMC_FECHAMATRICULA'] = $post_array['UMC_FECHAMATRICULA'];
        $connect_data['UMC_ESTADO'] = $post_array['UMC_ESTADO'];
        $connect_data['UMC_FECHADESACTIVACION'] = $post_array['UMC_FECHADESACTIVACION'];
        $connect_data['UMC_MOTIVO'] = $post_array['UMC_MOTIVO'];
        
        $update_enrollment=$this->db->update($this->table_name,$enrollment_data, array( $primary_key_field => $primary_key_value));
        $update_connect=$this->db->update("USUARIOMATRICULACONEXION",$connect_data, array( $primary_key_field => $primary_key_value));
    	return $update_enrollment&&$update_connect;
    }    
    
    function db_insert($post_array)
    {
        $enrollment_data=array();
        $connect_data=array();
        $MAC=array();
        
        $enrollment_data['EST_ID'] = $post_array['EST_ID'];
        $enrollment_data['MAT_DIRECCIONINSTALACION'] = $post_array['MAT_DIRECCIONINSTALACION'];
        $enrollment_data['MAT_ESTADOCONEXION'] = 'A';
        $enrollment_data['MAT_ESTADOMATRICULA'] = 'NP';
        $enrollment_data['TM_ID'] = $post_array['TM_ID'];
        
    	$insert = $this->db->insert("MATRICULA",$enrollment_data);
        if($insert)
    	{       
            $id=$this->db->insert_id();                

            $MAC['MAT_ID'] = $id;
            $MAC['CON_SERIAL'] = $post_array['CON_SERIAL'];
            date_default_timezone_set('America/Bogota');
            $MAC['MAC_FECHAADQUISICION'] = date('Y-m-d H:i:s');
            $insert = $this->db->insert("MATRICULAADQUIERECONTADOR",$MAC);

            $connect_data['MAT_ID'] = $id;
            $connect_data['USUA_ID'] = $post_array['USUA_ID'];
            date_default_timezone_set('America/Bogota');
            $connect_data['UMC_FECHAMATRICULA'] = date('Y-m-d H:i:s');
            $connect_data['UMC_ESTADO'] = 'A';
            $connect_data['UMC_FECHADESACTIVACION'] = $post_array['UMC_FECHADESACTIVACION'];
            $connect_data['UMC_MOTIVO'] = $post_array['UMC_MOTIVO'];
            $insert_UMC = $this->db->insert("USUARIOMATRICULACONEXION",$connect_data);
            if($insert_UMC){
                    return $id;
                }
                return false;
    	}
    	return false;
    }        
 
     function join_relation($field_name , $related_table , $related_field_title)
    {
		$related_primary_key = $this->get_primary_key($related_table);
		
		if($related_primary_key !== false)
                {
//                    if($field_name=='TM_ID')
//                    {
//                        $this->db->join('TIPOMATRICULA','TIPOMATRICULA.TM_ID=MATRICULA.TM_ID','LEFT OUTER');                        
//                        $this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
//                        return true;
//                    }
                    $unique_name = $this->_unique_join_name($field_name);
                    $this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left');
                    $this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
                    return true;
		}
    	
    	return false;
    }
    
}

?>